package vn.bus.service;

import java.util.ArrayList;
import java.util.Hashtable;

import com.google.gson.Gson;

import vn.bus.models.NearbyConnectionManager;
import vn.bus.dto.NearbyConnectionDTO;

public class NearbyConnectionService {

	public ArrayList<NearbyConnectionDTO> getAllNearbyConns() {
		ArrayList<NearbyConnectionDTO> allNearbyConns = null;
		try {
			NearbyConnectionManager nearbyConnManager = new NearbyConnectionManager();
			allNearbyConns = nearbyConnManager.getAllNearbyConns();
		} catch (Exception e) {
			System.out.println("error");
		}
		return allNearbyConns;
	}

	public String getAllNearbyConnsJson() {
		Gson gson = new Gson();
		return gson.toJson(getAllNearbyConns());
	}

	public Hashtable<String, ArrayList<NearbyConnectionDTO>> loadAllNodeNearbyConns() {

		Hashtable<String, ArrayList<NearbyConnectionDTO>> NodeNearbyConn = 
				new Hashtable<String, ArrayList<NearbyConnectionDTO>>();

		ArrayList<NearbyConnectionDTO> allNearbyConns = getAllNearbyConns();

		ArrayList<NearbyConnectionDTO> listNearbyConn;
		
		for(int i = 0; i < allNearbyConns.size(); i++){
			
			NearbyConnectionDTO nearbyConn = new NearbyConnectionDTO();
			nearbyConn = allNearbyConns.get(i);
			
			if(NodeNearbyConn.get(nearbyConn.getStation()) == null){
				listNearbyConn = new ArrayList<NearbyConnectionDTO>();
				listNearbyConn.add(nearbyConn);
				NodeNearbyConn.put(nearbyConn.getStation(), listNearbyConn);
			}else{
				listNearbyConn = new ArrayList<NearbyConnectionDTO>();
				listNearbyConn = NodeNearbyConn.get(nearbyConn.getStation());
				listNearbyConn.add(nearbyConn);
				NodeNearbyConn.put(nearbyConn.getStation(), listNearbyConn);
			}
		}
		return NodeNearbyConn;
	}
	
	public String updatePolyline(String polyline, String station, String nearby){
		String rs = "";
		try {
			NearbyConnectionManager nearbyConnManager = new NearbyConnectionManager();
			rs = nearbyConnManager.updatePolyline(polyline, station, nearby);
		} catch (Exception e) {
			System.out.println("error");
		}
		return rs;
	}
}
